package com.scsj.supermarket.view.activity.collectmodel;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.MineCollectionAdapter;
import com.scsj.supermarket.base.CollectBase;
import com.scsj.supermarket.bean.CommAlreadyBean;
import com.scsj.supermarket.bean.MyCollectGoodsBean;
import com.scsj.supermarket.bean.SelectCollectByUserBean;
import com.scsj.supermarket.config.RxSPConfig;
import com.scsj.supermarket.presenter.CollectPresenter;
import com.scsj.supermarket.view.fragment.basemodel.BaseFragment;
import com.vondear.rxtools.RxSPTool;
import com.vondear.rxtools.RxTool;

import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;
import okhttp3.RequestBody;

/**
 * 我的收藏界面
 */
public class MineCollectionFragment extends BaseFragment implements View.OnClickListener, CollectBase.IView {
    public static boolean isEditBtnClicked = false;
    public RelativeLayout bottomLayout;
    private RecyclerView recyclerView;
    private List<CommAlreadyBean> commAlreadyBeans;
    public MineCollectionAdapter mineCollectionAdapter;
    //用来判断是店铺收藏还是商品收藏 0:店铺 1:商品
    private int whichPage;
    private Context context;
    private TextView selectAllTv;
    private CheckBox checkAllCk;
    private TextView cancelBtn;
    private CollectPresenter presenter;
    private ArrayList<Boolean> isCheckList;
    private String userId;


    public MineCollectionFragment(int i, Context context) {
        super(i, context);
        this.whichPage = i;
        this.context = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_arecord_lauout, null);

        presenter = new CollectPresenter(this);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("","");
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonObject.toString());

//        presenter.deliverGoodsCollectData();
        initView(view, null);
        initData();
        initListener();
        return view;
    }



    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        isCheckList = new ArrayList<>();
        recyclerView = view.findViewById(R.id.application_list_rv);
        bottomLayout = view.findViewById(R.id.bottom_layout_rl);
        selectAllTv = view.findViewById(R.id.select_all_tv);
        cancelBtn = view.findViewById(R.id.delete_selected_item);
        checkAllCk = view.findViewById(R.id.shop_checkbox);
        checkAllCk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    //全选按钮
                    mineCollectionAdapter.setSelectAllItem(true);

                } else {
                    //取消全选
                    mineCollectionAdapter.setSelectAllItem(false);
                }
            }
        });
    }

    @Override
    protected void initListener() {
        cancelBtn.setOnClickListener(this);
    }

    @Override
    protected void initData() {

        userId = RxSPTool.getString(getHoldingActivity(), RxSPConfig.USER_ID);
        switch (whichPage) {
            case 0://店铺

//                presenter.deliverShopCollectData();
                break;
            case 1://商品
//                presenter.deliverGoodsCollectData();
                break;
            default:
                break;
        }

        commAlreadyBeans = new ArrayList<>();

        commAlreadyBeans.add(new CommAlreadyBean(R.mipmap.zzz_clothing, "交路口附近阿拉山口放假啊李开复阿萨冷风机", "获得254积分"));
        commAlreadyBeans.add(new CommAlreadyBean(R.mipmap.zzz_clothing, "交路口附近阿拉山口放假啊李开复阿萨冷风机", "获得254积分"));
        commAlreadyBeans.add(new CommAlreadyBean(R.mipmap.zzz_clothing, "交路口附近阿拉山口放假啊李开复阿萨冷风机", "获得254积分"));
        commAlreadyBeans.add(new CommAlreadyBean(R.mipmap.zzz_clothing, "交路口附近阿拉山口放假啊李开复阿萨冷风机", "获得254积分"));
        commAlreadyBeans.add(new CommAlreadyBean(R.mipmap.zzz_clothing, "交路口附近阿拉山口放假啊李开复阿萨冷风机", "获得254积分"));
        commAlreadyBeans.add(new CommAlreadyBean(R.mipmap.zzz_clothing, "交路口附近阿拉山口放假啊李开复阿萨冷风机", "获得254积分"));
        commAlreadyBeans.add(new CommAlreadyBean(R.mipmap.zzz_clothing, "交路口附近阿拉山口放假啊李开复阿萨冷风机", "获得254积分"));
        commAlreadyBeans.add(new CommAlreadyBean(R.mipmap.zzz_clothing, "交路口附近阿拉山口放假啊李开复阿萨冷风机", "获得254积分"));
        commAlreadyBeans.add(new CommAlreadyBean(R.mipmap.zzz_clothing, "交路口附近阿拉山口放假啊李开复阿萨冷风机", "获得254积分"));
        commAlreadyBeans.add(new CommAlreadyBean(R.mipmap.zzz_clothing, "交路口附近阿拉山口放假啊李开复阿萨冷风机", "获得254积分"));
        commAlreadyBeans.add(new CommAlreadyBean(R.mipmap.zzz_clothing, "交路口附近阿拉山口放假啊李开复阿萨冷风机", "获得254积分"));
//        mineCollectionAdapter = new MineCollectionAdapter(commAlreadyBeans, whichPage, getHoldingActivity());
        for (int i = 0; i < 6; i++) {
            commAlreadyBeans.add(new CommAlreadyBean(R.mipmap.zzz_clothing, "交路口附近阿拉山口放假啊李开复阿萨冷风机", "获得254积分", false));
            isCheckList.add(false);
        }
        mineCollectionAdapter = new MineCollectionAdapter(commAlreadyBeans, isCheckList, whichPage, getHoldingActivity());
        LinearLayoutManager layout = new LinearLayoutManager(getHoldingActivity());
        layout.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layout);
        recyclerView.setAdapter(mineCollectionAdapter);
        mineCollectionAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, final int position) {

                CommAlreadyBean item = (CommAlreadyBean) adapter.getItem(position);
                CheckBox check = view.findViewById(R.id.shop_checkbox);
                if (item.isChecked()) {
                    check.setChecked(false);
                } else {
                    check.setChecked(true);
                }
                isCheckList.set(position, true);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.delete_selected_item:
                Toasty.info(context, "删除按钮点击").show();
//                mineCollectionAdapter.deleteSelectedItem();
                mineCollectionAdapter.deleteSelectedItem(isCheckList);
                break;
        }
    }

    @Override
    public void showShopCollectSuccessMsg(String s, SelectCollectByUserBean selectCollectByUserBean) {

    }

    @Override
    public void showShopAddAndDelCollectSuccessMsg(String s) {

    }

    @Override
    public void showGoodsCollectSuccessMsg(String s, MyCollectGoodsBean myCollectGoodsBean) {

    }

    @Override
    public void showGoodsAddCollectSuccessMsg(String s) {

    }

    @Override
    public void showGoodsDelCollectSuccessMsg(String s) {

    }

    @Override
    public void showErrorMsg(String s) {
        Toasty.error(context,s).show();
    }
}
